//
//  NewFeatureViewController.swift
//  01-WeiBo
//
//  Created by 李晓东 on 17/7/2.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "NewFeatureViewCell"
private let WBNewFeatureImageCount = 4
class NewFeatureViewController: UICollectionViewController {

    init() {
        var layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.register(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBNewFeatureImageCount
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewFeatureViewCell
    
        cell.backgroundColor = UIColor.blue
        cell.imageIndex = indexPath.item
        print(cell)
        return cell
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / (scrollView.frame.width))
        if page != WBNewFeatureImageCount - 1 {
            return
        }
       let indexPath = NSIndexPath(item: page, section: 0)
        let cell = collectionView?.cellForItem(at: indexPath as IndexPath) as! NewFeatureViewCell
        cell.showLoginButton()
    }

}

import SnapKit
class NewFeatureViewCell:UICollectionViewCell{
    
    fileprivate var imageIndex : Int = 0{
        didSet{
            viewUI.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            loginButton.isHidden = true
        }
    }
    
//    fileprivate var loginButton : UIButton{
//        let btn = UIButton(titleName: "登录", color:  UIColor.blue, imageName: "new_feature_finish_button")
//        
////        btn.isHidden = true
////        self.addSubview(btn)
//        return btn
//    }
    
    fileprivate var loginButton :UIButton = UIButton(titleName: "登录", color:  UIColor.blue, imageName: "new_feature_finish_button")
    
    fileprivate func showLoginButton(){
        loginButton.isHidden = false
        loginButton.isUserInteractionEnabled = false
        loginButton.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        UIView.animate(withDuration: 10.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: {
            self.loginButton.transform = CGAffineTransform.identity
        }) { (_) in
            self.loginButton.isUserInteractionEnabled = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var viewUI = UIImageView()
    
    func setupUI() {
        viewUI.frame = self.bounds
        viewUI.backgroundColor = UIColor.yellow
        self.addSubview(viewUI)
        // 这里的 会访问一次loginButton 的 get 方法,返回一个新的 button
        self.addSubview(loginButton)
        
        // 而这里又会再访问一次 get 方法,又返回一个新的 button
        // 这时候,不该再用86行那种初始化方法了.而应该使用94行的那种
        loginButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(viewUI.snp.centerX)
            make.bottom.equalTo(viewUI.snp.bottom).multipliedBy(0.7)
        }
        loginButton.addTarget(self, action: "jumpToMainViewController", for: .touchUpInside)
        
    }
    
    func jumpToMainViewController() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBSwitchRootViewControllerNotification), object: nil)
    }
}
